{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "gold_data=pd.read_csv('LBMA-GOLD.csv')\n",
    "bit_data=pd.read_csv('BCHAIN-MKPRU.csv')\n",
    "a_g=np.array(np.array(range(1,len(gold_data['Date'])+1)))\n",
    "b_g=np.array(gold_data['Date'])\n",
    "a_b=np.array(np.array(range(1,len(bit_data['Date'])+1)))\n",
    "b_b=np.array(bit_data['Date'])\n",
    "table_g=np.vstack([a_g,b_g]).T\n",
    "table_b=np.vstack([a_b,b_b]).T\n",
    "\n",
    "def dayb2dayg(day_b):\n",
    "    try:\n",
    "        for li in table_b:\n",
    "            if li[0]==day_b:\n",
    "                date=li[1]\n",
    "        for li in table_g:\n",
    "            if li[1]==date:\n",
    "                day_g=li[0]\n",
    "        return day_g,date\n",
    "    except:\n",
    "        return (False,date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "bit_data=np.array(bit_data)\n",
    "gold_data=np.array(gold_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pre_g=pd.read_csv('prediction_g.csv',header=None)\n",
    "pre_b=pd.read_csv('prediction_b.csv',header=None)\n",
    "pre_g=np.array(pre_g)\n",
    "pre_g[:,0].astype(int)\n",
    "pre_b=np.array(pre_b)\n",
    "pre_b[:,0].astype(int)\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "609.96"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre_b[5-5][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "设天数为day_b取该日两产品价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# today is day_b (day_b>=5)\n",
    "def day_info(day_b):\n",
    "    if day_b<5:\n",
    "        raise ValueError('day_b must bigger than 5')\n",
    "    if dayb2dayg(day_b)[0]!=False:\n",
    "        (day_g,date)=dayb2dayg(day_b)\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        value_g=gold_data[day_g-1][1]\n",
    "        value_g_p=pre_g[day_g-5][1]\n",
    "        value_g_pp=pre_g[day_g-5][2]\n",
    "        info=(True,value_g,value_g_p,value_g_pp,value_b,value_b_p,value_b_pp,date)\n",
    "        return info\n",
    "    else: # gold cannot be treated this day\n",
    "        date=dayb2dayg(day_b)[1]\n",
    "        value_b=bit_data[day_b-1][1]\n",
    "        value_b_p=pre_b[day_b-5][1]\n",
    "        value_b_pp=pre_b[day_b-5][2]\n",
    "        info=(False,value_b,value_b_p,value_b_pp,date)\n",
    "        return info\n",
    "\n",
    "def day_info2(day_b):\n",
    "    if day_b<5:\n",
    "        raise ValueError('day_b must bigger than 5')\n",
    "    if dayb2dayg(day_b)[0]!=False:\n",
    "        (day_g,date)=dayb2dayg(day_b)\n",
    "        try:\n",
    "            value_b=bit_data[day_b-1][1]\n",
    "            value_b_p=bit_data[day_b][1]\n",
    "            value_b_pp=bit_data[day_b+100][1]\n",
    "        except:\n",
    "            value_b=bit_data[day_b-1][1]\n",
    "            value_b_p=bit_data[-1][1]\n",
    "            value_b_pp=bit_data[-1][1]    \n",
    "        try:        \n",
    "            value_g=gold_data[day_g-1][1]\n",
    "            value_g_p=gold_data[day_g][1]\n",
    "            value_g_pp=gold_data[day_g+100][1]\n",
    "        except:\n",
    "            value_g=gold_data[day_g-1][1]\n",
    "            value_g_p=gold_data[-1][1]       \n",
    "            value_g_pp=gold_data[-1][1]     \n",
    "        info=(True,value_g,value_g_p,value_g_pp,value_b,value_b_p,value_b_pp,date)\n",
    "        return info\n",
    "    else: # gold cannot be treated this day\n",
    "        date=dayb2dayg(day_b)[1]\n",
    "        try:\n",
    "            value_b=bit_data[day_b-1][1]\n",
    "            value_b_p=bit_data[day_b][1]\n",
    "            value_b_pp=bit_data[day_b+100][1]\n",
    "        except:\n",
    "            value_b=bit_data[day_b-1][1]\n",
    "            value_b_p=bit_data[-1][1]\n",
    "            value_b_pp=bit_data[-1][1]          \n",
    "        info=(False,value_b,value_b_p,value_b_pp,date)\n",
    "        return info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 1251.1, 1248.8, 1286.65, 1040.5755, 1037.90455, 2502.86, '3/29/17')"
      ]
     },
     "execution_count": 225,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day_info2(200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 1794.6, 1817.6, 1836.1, 46368.69, 47281.0, 48644.0, '9/10/21')"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day_info(1826)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [],
   "source": [
    "# alpha_g=0.01\n",
    "# alpha_g=0.02\n",
    "# n_c=1000\n",
    "# n_g=100\n",
    "# n_b=100\n",
    "# n_c_i=0\n",
    "# n_g_i=0\n",
    "# n_b_i=0\n",
    "# R=10\n",
    "# for day_b in range(6,1827,50):\n",
    "#     info=day_info2(day_b)\n",
    "#     if info[0]==True: # gold can be treated\n",
    "#         g_increase1=(info[2]-info[1])/info[1]\n",
    "#         g_increase2=(info[3]-info[1])/info[1]\n",
    "#         b_increase1=(info[5]-info[4])/info[4]\n",
    "#         b_increase2=(info[6]-info[4])/info[4]\n",
    "\n",
    "#         n_g_i+=(0+10*g_increase1)*R\n",
    "#         n_c-=(n_g_i-n_g)*info[1]\n",
    "#         n_b_i+=(0+10*b_increase1)*R\n",
    "#         n_c-=(n_b_i-n_b)*info[4]\n",
    "#         # n_c_i=n_c-(n_g_i-n_g)*info[1]-(n_b_i-n_b)*info[4]\n",
    "#         n_g=n_g_i\n",
    "#         n_b=n_b_i\n",
    "\n",
    "\n",
    "#         # print(g_increase1,b_increase1)\n",
    "#     elif info[0]==False:\n",
    "#         b_increase1=(info[2]-info[1])/info[1]\n",
    "#         b_increase2=(info[3]-info[1])/info[1]\n",
    "#         if b_increase1>0:\n",
    "#             pass\n",
    "#         if b_increase1<0:\n",
    "#             pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-2.4189037863117063e+242\n"
     ]
    }
   ],
   "source": [
    "# info=day_info(1825)\n",
    "# sum=n_c_i+n_g_i*info[1]+n_b_i*info[4]\n",
    "# # print(sum)\n",
    "# print(n_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, 1136.25, 1215.2, 1257.4, 788.67, 895.798875, 1037.90455, '12/19/16')"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day_info2(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_c=1000\n",
    "n_g=0\n",
    "n_b=0\n",
    "R=1\n",
    "for day_b in range(6,1827):\n",
    "    info=day_info2(day_b)\n",
    "    if info[0]==True: # gold can be treated\n",
    "        g_increase1=(info[2]-info[1])/info[1]\n",
    "        # g_increase2=(info[3]-info[1])/info[1]\n",
    "        b_increase1=(info[5]-info[4])/info[4]\n",
    "        # b_increase2=(info[6]-info[4])/info[4]\n",
    "        if n_c>500 and n_g>10 and n_b>10:\n",
    "            if g_increase1>0:\n",
    "                n_g_i=n_g+R\n",
    "                n_c_i=n_c-R*info[1]\n",
    "            if g_increase1<0:\n",
    "                n_g_i=n_g-R\n",
    "                n_c_i=n_c+R*info[1]\n",
    "            if b_increase1>0:\n",
    "                n_b_i=n_b+R\n",
    "                n_c_i=n_c_i-R*info[4]   \n",
    "                print(n_c_i,info[4])    \n",
    "            if b_increase1<0:\n",
    "                n_b_i=n_b-R\n",
    "                n_c_i=n_c_i+R*info[4]      \n",
    "            n_g=n_g_i\n",
    "            n_b=n_b_i\n",
    "            n_c=n_c_i\n",
    "        else:\n",
    "            # if g_increase1<-0.1:\n",
    "            #     n_g_i=n_g-1\n",
    "            #     n_c_i=n_c+1*info[1]\n",
    "            # if g_increase1<-0.1:\n",
    "            #     n_b_i=n_b-1\n",
    "            #     n_c_i=n_c+1*info[4]    \n",
    "            n_b_i=0.3*n_b \n",
    "            n_c_i=n_c+(n_b_i-n_b)*info[4]\n",
    "\n",
    "            n_g_i=n_g+1\n",
    "            n_c_i=n_c_i-1*info[1] \n",
    "\n",
    "            n_g=n_g_i\n",
    "            n_b=n_b_i\n",
    "            n_c=n_c_i\n",
    "            # print(g_increase1,b_increase1)\n",
    "    elif info[0]==False:\n",
    "        b_increase1=(info[2]-info[1])/info[1]\n",
    "        b_increase2=(info[3]-info[1])/info[1]\n",
    "        if b_increase1>0:\n",
    "            pass\n",
    "        if b_increase1<0:\n",
    "            pass   \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-232383.5732470699"
      ]
     },
     "execution_count": 223,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_c=1000\n",
    "n_g=0\n",
    "n_b=0\n",
    "R=0.1\n",
    "for day_b in range(6,1827):\n",
    "    info=day_info2(day_b)\n",
    "    if info[0]==True: # gold can be treated\n",
    "        g_increase1=(info[2]-info[1])/info[1]\n",
    "        b_increase1=(info[5]-info[4])/info[4]\n",
    "        if g_increase1>0:\n",
    "            n_g_i=n_g+g_increase1*R\n",
    "            cash_g_m=g_increase1*R*info[1]\n",
    "            n_c-=cash_g_m\n",
    "        if b_increase1>0:\n",
    "            n_b_i=n_b+b_increase1*R\n",
    "            cash_b_m=b_increase1*R*info[4]\n",
    "            n_c-=cash_b_m\n",
    "        n_c_i=n_c\n",
    "        n_g=n_g_i\n",
    "        n_b=n_b_i\n",
    "        if n_c_i<800:\n",
    "            n_g_i=n_g-0.05\n",
    "            cash_g_p=0.1*info[1]\n",
    "            n_b_i=n_b-0.05\n",
    "            cash_b_p=0.1*info[4]\n",
    "            n_c_i=n_c+cash_b_p+cash_g_p\n",
    "            n_g=n_g_i\n",
    "            n_b=n_b_i\n",
    "n_c_i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "n_c=1000\n",
    "n_g=0\n",
    "n_b=0\n",
    "R=0.01\n",
    "list_c=[]\n",
    "for day_b in range(6,1827,2):\n",
    "    info=day_info2(day_b)\n",
    "    if info[0]==True: # gold can be treated\n",
    "        g_increase1=(info[2]-info[1])/info[1]\n",
    "        b_increase1=(info[5]-info[4])/info[4]   \n",
    "        if g_increase1>0:\n",
    "            n_g+=R*10*g_increase1\n",
    "            n_c+=R*10*g_increase1*(info[2]-info[1])\n",
    "        if b_increase1>0:\n",
    "            n_b+=R*10*b_increase1\n",
    "            n_c+=R*10*b_increase1*(info[5]-info[4])\n",
    "            "
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "b5fc3e43e814ad01a3162358639356c8d5269fa9e7a0a06af2b9cdf0ba130cad"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
